﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace TowerDefense.UI
{
    public class ButtonEventArgs : EventArgs
    {
        public int Position {get;set;}
    }

    public delegate void ButtonEventHandler(object sender, ButtonEventArgs e);

    public class Button : Entity
    {
        public int Height { get; set; }
        public int Width { get; set; }
        public ButtonState ButtonState { get; set; }

        public event ButtonEventHandler Click;
        event ButtonEventHandler MouseEnter;
        event ButtonEventHandler MouseLeave;

        public Texture2D ButtonDownTexture { get; set; }
        public Texture2D ButtonMouseOverTexture { get; set; }

        public Button(int height, int width)
        {
            this.ButtonState = ButtonState.Idle;
            this.Height = height;
            this.Width = width;
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            MouseState mstate = Mouse.GetState();

            if (mstate.X >= this.Position.X && mstate.Y >= this.Position.Y && mstate.X <= this.Position.X + this.Width && mstate.Y <= this.Position.Y + this.Height)
            {
                if (mstate.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                {
                    if (this.ButtonState == ButtonState.MouseOver || this.ButtonState == ButtonState.Idle)
                    {
                        if(Click != null)
                            Click(this, new ButtonEventArgs()); 
                    }
                    this.ButtonState = ButtonState.Pressed;
                }
                else
                {
                    this.ButtonState = ButtonState.MouseOver;
                }
            }
            else
            {
                this.ButtonState = ButtonState.Idle;
            }
        }

        public override Entity Clone()
        {
            throw new NotImplementedException();
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {

            switch (this.ButtonState)
            {
                case ButtonState.Pressed:
                    spriteBatch.Draw(this.ButtonDownTexture, new Rectangle((int)this.Position.X, (int)this.Position.Y, this.Width, this.Height), Color.White);
                    break;
                case ButtonState.MouseOver:
                    spriteBatch.Draw(this.ButtonMouseOverTexture, new Rectangle((int)this.Position.X, (int)this.Position.Y, this.Width, this.Height), Color.White);
                    break;
                case ButtonState.Idle:
                    spriteBatch.Draw(this.Texture, new Rectangle((int)this.Position.X, (int)this.Position.Y, this.Width, this.Height), Color.White);
                    break;
                default:
                    break;
            }
        }
    }

    public enum ButtonState
    {
        Pressed,
        MouseOver,
        Idle
    }

    public static class ButtonHandler
    {
        private static List<Button> _buttons = new List<Button>();

        public static List<Button> Buttons
        {
            get
            {
                return _buttons;
            }
            set
            {
                _buttons = value;
            }
        }



        public static void Update(GameTime gameTime)
        {
            foreach (Button button in Buttons)
            {
                button.Update(gameTime);
            }
        }
        public static void Draw(SpriteBatch spriteBatch)
        {
            foreach (Button button in Buttons)
            {
                button.Draw(spriteBatch);
            }
        }
    }
}
